// ========== Quick Docs & Notes Section ========== //
const QUICK_DOCS_KEY = 'taskTrackerQuickDocs';
const QUICK_NOTES_KEY = 'taskTrackerQuickNotes';

// 通用通知方法，参考 home.js
function showNotification(message, type = 'success', duration = 3000) {
  const notification = document.getElementById('notification');
  const notificationMessage = document.getElementById('notification-message');
  // Set message content
  notificationMessage.textContent = message;
  // Ensure notification element type is correct
  notification.className = 'notification ' + type;
  // Show notification
  notification.classList.add('show');
  // Set timer to automatically hide notification
  setTimeout(() => {
    // Apply fade out animation
    notification.style.animation = 'notification-hide 0.4s forwards';
    setTimeout(() => {
      notification.classList.remove('show');
      notification.style.animation = '';
    }, 400);
  }, duration);
}

function loadQuickDocs() {
  const docs = JSON.parse(localStorage.getItem(QUICK_DOCS_KEY) || '[]');
  return Array.isArray(docs) ? docs : [];
}

function saveQuickDocs(docs) {
  localStorage.setItem(QUICK_DOCS_KEY, JSON.stringify(docs));
}

function renderQuickDocs() {
  const docs = loadQuickDocs();
  const list = document.getElementById('quick-docs-list');
  list.innerHTML = '';
  docs.forEach((doc, idx) => {
    const li = document.createElement('li');
    li.className = 'quick-doc-item';
    li.style.display = 'flex';
    li.style.alignItems = 'center';
    li.style.maxWidth = '100%';
    li.style.flex = '0 1 auto';
    li.style.minWidth = '0';
    const link = document.createElement('a');
    link.href = doc.url;
    link.target = '_blank';
    link.rel = 'noopener noreferrer';
    link.textContent = doc.title || doc.url;
    link.className = 'quick-doc-link';
    link.style.flex = '1 1 auto';
    link.style.overflow = 'hidden';
    link.style.textOverflow = 'ellipsis';
    link.style.whiteSpace = 'nowrap';
    link.style.minWidth = '0';
    li.appendChild(link);
    const delBtn = document.createElement('button');
    delBtn.className = 'icon-button quick-doc-delete';
    delBtn.title = 'Delete';
    delBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>';
    delBtn.style.alignSelf = 'baseline';
    delBtn.style.marginLeft = '6px';
    delBtn.style.paddingBottom = '0';
    delBtn.onclick = function() {
      docs.splice(idx, 1);
      saveQuickDocs(docs);
      renderQuickDocs();
    };
    li.appendChild(delBtn);
    list.appendChild(li);
  });
}

function setupQuickDocsEvents() {
  document.getElementById('save-doc-link-btn').onclick = function() {
    const title = document.getElementById('doc-link-title').value.trim();
    const url = document.getElementById('doc-link-url').value.trim();
    if (!url) {
      showNotification('Please enter a valid URL.', 'error');
      return;
    }
    const docs = loadQuickDocs();
    docs.push({ title: title || url, url });
    saveQuickDocs(docs);
    renderQuickDocs();
    document.getElementById('doc-link-title').value = '';
    document.getElementById('doc-link-url').value = '';
  };
}

function loadQuickNotes() {
  const notes = JSON.parse(localStorage.getItem(QUICK_NOTES_KEY) || '[]');
  return Array.isArray(notes) ? notes : [];
}

function saveQuickNotes(notes) {
  localStorage.setItem(QUICK_NOTES_KEY, JSON.stringify(notes));
}

function renderQuickNotes() {
  const notes = loadQuickNotes();
  const list = document.getElementById('quick-notes-list');
  list.innerHTML = '';
  notes.forEach((note, idx) => {
    const li = document.createElement('li');
    li.className = 'quick-note-item';
    li.style.display = 'flex';
    li.style.alignItems = 'baseline';
    li.style.maxWidth = '100%';
    li.style.flex = '0 1 auto';
    li.style.minWidth = '0';
    li.style.position = 'relative';
    const textDiv = document.createElement('div');
    textDiv.className = 'quick-note-text';
    textDiv.textContent = note.text;
    textDiv.style.flex = '1 1 auto';
    textDiv.style.wordBreak = 'break-all';
    textDiv.style.minWidth = '0';
    textDiv.style.cursor = 'text';
    textDiv.title = 'Click to edit';
    textDiv.onclick = function(e) {
      e.stopPropagation();
      if (textDiv.isContentEditable) return;
      textDiv.contentEditable = 'true';
      textDiv.focus();
    };
    textDiv.onblur = function() {
      textDiv.contentEditable = 'false';
      const notes = loadQuickNotes();
      notes[idx].text = textDiv.textContent.trim();
      saveQuickNotes(notes);
      renderQuickNotes();
    };
    li.appendChild(textDiv);
    // Delete button
    const delBtn = document.createElement('button');
    delBtn.className = 'icon-button quick-note-delete';
    delBtn.title = 'Delete';
    delBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>';
    delBtn.style.position = 'absolute';
    delBtn.style.top = '4px';
    delBtn.style.right = '4px';
    delBtn.style.margin = '0';
    delBtn.style.padding = '2px';
    delBtn.style.width = '20px';
    delBtn.style.height = '20px';
    delBtn.onclick = function() {
      const notes = loadQuickNotes();
      notes.splice(idx, 1);
      saveQuickNotes(notes);
      renderQuickNotes();
      showNotification('Note deleted', 'success');
    };
    li.appendChild(delBtn);
    list.appendChild(li);
  });
}

function setupQuickNotesEvents() {
  document.getElementById('save-note-btn').onclick = function() {
    const input = document.getElementById('note-input');
    const text = input.value.trim();
    if (!text) {
      showNotification('Please enter a note.', 'error');
      return;
    }
    const notes = loadQuickNotes();
    notes.push({ text });
    saveQuickNotes(notes);
    renderQuickNotes();
    input.value = '';
  };
}

document.addEventListener('DOMContentLoaded', function() {
  renderQuickDocs();
  setupQuickDocsEvents();
  renderQuickNotes();
  setupQuickNotesEvents();
}); 